package com.example.androidproject.utils;

import android.app.Fragment;
import android.os.Looper;
import android.util.Log;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.RecyclerView;

import java.util.concurrent.CountDownLatch;

public class Tool {

    /**
     * 判断recycleView是否到达底部
     *
     * @param rv recycleView控件
     * @return true 到底部了 false 没到达底部
     */
    public static boolean isSlideToBottom(RecyclerView rv) {
        if (rv == null)
            return false;
        if (rv.computeVerticalScrollExtent() + rv.computeVerticalScrollOffset() >= rv.computeVerticalScrollRange())
            return true;
        return false;
    }

    /**
     * 控件dialog 居中
     * 父组件为RelativeLayout
     */
    public static void setDiaLogCenter(LinearLayout llDialog, RelativeLayout rlvParent) {
        ViewTreeObserver observer = llDialog.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                llDialog.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                // 获取父组件的margin值
                ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) rlvParent.getLayoutParams();
                int marginLeft = params.leftMargin;
                int marginTop = params.topMargin;
                int width = llDialog.getWidth();
                int height = llDialog.getMeasuredHeight();
                int centerW = (int) (GlobalVariable.screenWidth / 2 - width / 2) - marginLeft;
                int centerH = (int) (GlobalVariable.screenHeight / 2 - height / 2) - marginTop;
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams((int) (80 * GlobalVariable.scaledDensity), (int) (70 * GlobalVariable.scaledDensity));
                layoutParams.setMargins(centerW, centerH, 0, 0);
                llDialog.setLayoutParams(layoutParams);
            }
        });

    }

    /**
     * 父组件为LinearLayout
     */
    public static void setDiaLogCenter(LinearLayout llDialog, LinearLayout llParent) {
        ViewTreeObserver observer = llDialog.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                llDialog.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) llParent.getLayoutParams();
                int marginLeft = params.leftMargin;
                int marginTop = params.topMargin;
                int width = llDialog.getWidth();
                int height = llDialog.getMeasuredHeight();
                int centerW = (int) (GlobalVariable.screenWidth / 2 - width / 2) - marginLeft;
                int centerH = (int) (GlobalVariable.screenHeight / 2 - height / 2) - marginTop;
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams((int) (80 * GlobalVariable.scaledDensity), (int) (70 * GlobalVariable.scaledDensity));
                layoutParams.setMargins(centerW, centerH, 0, 0);
                llDialog.setLayoutParams(layoutParams);
            }
        });
    }


}
